package com.retailcloud.thirdpartyservice.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.ocr_api20210707.models.RecognizeGeneralStructureResponse;
import com.aliyun.tea.TeaException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author wudi
 * @date 2024/11/5
 * @comment
 */
@Component
public class AliOcrUtil {

    @Autowired
    private com.aliyun.ocr_api20210707.Client aliOcrClient;


    public Map<String,String> getTxt(String url) {
        com.aliyun.ocr_api20210707.models.RecognizeGeneralStructureRequest recognizeGeneralStructureRequest = new com.aliyun.ocr_api20210707.models.RecognizeGeneralStructureRequest()
                .setUrl(url);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions()
                .setReadTimeout(5000000)
                .setConnectTimeout(5000000);
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeGeneralStructureResponse recognizeGeneralStructureResponse = aliOcrClient.recognizeGeneralStructureWithOptions(recognizeGeneralStructureRequest, runtime);
            return JSONObject.parseObject(JSONObject.toJSONString(recognizeGeneralStructureResponse.getBody().getData().getSubImages().get(0).getKvInfo().getData()),Map.class) ;
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }


}
